package demo3;

import java.util.Objects;

class Person{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
        //return "x";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    /*    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(this == obj){
            return ture;
        }
        //不是一个Person类对象
        if(!(obj instanceof  Person)){
            return false;
        }
        Person person = (Person) obj;//向下转型，比较属性值
        return this.name.equals(person.name)&&this.age == person.age;
    }*/
}

public class Test {
    public static void main(String[] args) {
        Person person1 = new Person("张三",10);
        System.out.println(person1);
        Person person2 = new Person("张三",10);
        System.out.println(person2);
        System.out.println("=======");
       /* //此时比较的是地址
        System.out.println(person1  == person2);//false   false
        System.out.println(person1.equals(person2));//false   true
        Person person3 = person1;
        System.out.println(person1.equals(person3));//true   true*/
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());//两值相等
        //后期降到哈希表的时候 就知道 两个一样的对象我们想放在一个位置此时就可以利用重写这个方法来做

    }

}
